// const Promise = require('./Promise')

const formatTime = (date) => {
  const year = date.getFullYear();
  const month = date.getMonth() + 1;
  const day = date.getDate();
  const hour = date.getHours();
  const minute = date.getMinutes();
  const second = date.getSeconds();

  return (
    [year, month, day].map(formatNumber).join("-") +
    " " +
    [hour, minute, second].map(formatNumber).join(":")
  );
};

const formatNumber = (n) => {
  n = n.toString();
  return n[1] ? n : "0" + n;
};

//金额格式
const formatCurrency = (input) => {
  return input.toFixed(2).replace(/\d(?=(\d{3})+\.)/g, "$&");
};
//更新url参数
const updateQueryStringParameter = (uri, key, value) => {
  var re = new RegExp("([?&])" + key + "=.*?(&|$)", "i");
  var separator = uri.indexOf("?") !== -1 ? "&" : "?";
  if (uri.match(re)) {
    return uri.replace(re, "$1" + key + "=" + value + "$2");
  } else {
    return uri + separator + key + "=" + value;
  }
};

//生成不重复的字符串
const random_string = () => {
  let idStr = Date.now().toString(36);
  idStr += Math.random()
    .toString(36)
    .substr(3);
  return idStr;
};
// 把字符串中间的文本替换成 3个*
const replaceStr = (strHide, type) => {
  // 隐秘字符产中间的部分的内容 type:1；只替换中间的文本 2：替换全部的文本
  var reg = /^(.{2})(.*)(.{2})$/;

  strHide = strHide.replace(reg, function(a, b, c, d) {
    return b + c.replace(/./g, "*") + d;
  });
  // strHide = strHide.replace(/\*+/g, "***")
  if (type == 1) {
    strHide = strHide.replace(/\*+/g, "******");
  } else {
    strHide = strHide.replace(strHide, "******");
  }
  return strHide;
};
// 格式化
const getNewDate = (time, mode) => {
  // 重新优化选择的事件的格式 mode:1:只取月日 2：取年月日
  var timestamp = Date.parse(time);
  var date = new Date(timestamp);
  //获取年份
  var Y = date.getFullYear();
  //获取月份
  var M =
    date.getMonth() + 1 < 10
      ? "0" + (date.getMonth() + 1)
      : date.getMonth() + 1;
  //获取当日日期
  var D = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
  //获取小时
  var hour = date.getHours() < 10 ? "0" + date.getHours() : date.getHours();
  //获取分钟
  var minute =
    date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes();
  //获取秒
  var second =
    date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
  var nowTime;
  if (mode == 1) {
    return (nowTime = Y + "-" + M + "-" + D);
  } else if (mode == 2) {
    return Y + "年" + M + "月" + D + "日";
  } else {
    return (
      Y + "年" + M + "月" + D + "日" + " " + hour + ":" + minute + ":" + second
    );
  }
};

// 通过时间戳获取到现在时间
// 20201008 施大威 这谁写的函数注释?? 明明是格式化日期, 返回字符串的啊!
// mode  格式1:2020-09-15   格式2:2020年09月15日
const getNowDateTime = (timestamp, mode) => {
  var date = new Date(timestamp);
  var Y = date.getFullYear();
  var M =
    date.getMonth() + 1 < 10
      ? "0" + (date.getMonth() + 1)
      : date.getMonth() + 1;
  var D = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
  // var Y = date.getFullYear() + "-";
  // var M =
  //   (date.getMonth() + 1 < 10
  //     ? "0" + (date.getMonth() + 1)
  //     : date.getMonth() + 1) + "-";
  // var D =
  //   (date.getDate() < 10 ? "0" + date.getDate() : date.getDate()) + "";
  let str;
  // mode   1是=  2是年月日格式
  if (mode == 1) {
    str = Y + "-" + M + "-" + D + "";
  } else if (mode == 2) {
    str = Y + "年" + M + "月" + D + "日";
  }
  return str;
};

// 日期转时间戳
const dateToTimeStamp = (date) => {
  let result = new Date(date).getTime();
  return result;
};

// 获取今天的日期
const getLatelyTodayDate = () => {
  let now = new Date(); //当前日期
  let nowDay = now.getDate(); //当前日
  let nowMonth = now.getMonth(); //当前月
  let nowYear = now.getYear(); //当前年
  nowYear += nowYear < 2000 ? 1900 : 0; //
  let today = new Date(nowYear, nowMonth, nowDay);
  return formatDate(today);
};

// 获取昨天的日期
const getLatelyYesterdayDate = () => {
  let now = new Date(); //当前日期
  let nowYesterday = new Date(now.getTime() - 24 * 60 * 60 * 1000).getDate(); // 昨天
  let nowDay = now.getDate(); //当前日
  let nowMonth = now.getMonth(); //当前月
  let nowYear = now.getYear(); //当前年
  nowYear += nowYear < 2000 ? 1900 : 0; //
  let yesterday = new Date(nowYear, nowMonth, nowYesterday);
  return formatDate(yesterday);
};
//获得本月的开始日期和结束日期
const getMonthStartDateAndMonthEndDate = () => {
  let now = new Date(); //当前日期
  let nowMonth = now.getMonth(); //当前月
  let nowYear = now.getYear(); //当前年
  nowYear += nowYear < 2000 ? 1900 : 0; //
  //获得某月的天数
  function getMonthDays(myMonth) {
    let monthStartDate = new Date(nowYear, myMonth, 1);
    let monthEndDate = new Date(nowYear, myMonth + 1, 1);
    let days = (monthEndDate - monthStartDate) / (1000 * 60 * 60 * 24);
    return days;
  }
  let monthStartDate = new Date(nowYear, nowMonth, 1);
  let monthEndDate = new Date(nowYear, nowMonth, getMonthDays(nowMonth));
  return [formatDate(monthStartDate), formatDate(monthEndDate)];
};

// 获取当前季度的范围日期
const getQuarterStartDateAndMonthEndDate = () => {
  let now = new Date(); //当前日期
  let nowMonth = now.getMonth(); //当前月
  let nowYear = now.getYear(); //当前年
  nowYear += nowYear < 2000 ? 1900 : 0;
  let quarterStartDate = new Date(nowYear, getQuarterStartMonth(), 1);
  let quarterEndMonth = getQuarterStartMonth() + 2;
  let quarterEndDate = new Date(
    nowYear,
    quarterEndMonth,
    getMonthDays(quarterEndMonth)
  );

  //获得本季度的开始月份
  function getQuarterStartMonth() {
    let quarterStartMonth = 0;
    if (nowMonth < 3) {
      quarterStartMonth = 0;
    }
    if (2 < nowMonth && nowMonth < 6) {
      quarterStartMonth = 3;
    }
    if (5 < nowMonth && nowMonth < 9) {
      quarterStartMonth = 6;
    }
    if (nowMonth > 8) {
      quarterStartMonth = 9;
    }
    return quarterStartMonth;
  }

  //获得某月的天数
  function getMonthDays(myMonth) {
    let monthStartDate = new Date(nowYear, myMonth, 1);
    let monthEndDate = new Date(nowYear, myMonth + 1, 1);
    let days = (monthEndDate - monthStartDate) / (1000 * 60 * 60 * 24);
    return days;
  }
  return [formatDate(quarterStartDate), formatDate(quarterEndDate)];
};

// 得到当前周的范围日期
function getWeekStartDateAndMonthEndDate() {
  let now = new Date(); //当前日期
  let nowDayOfWeek = now.getDay(); //今天本周的第几天
  let timeStamp = now.getTime(); // 距 1970 年 1 月 1 日之间的毫秒数：
  let millisecond = 1000 * 60 * 60 * 24; //一天的毫秒数
  let minusDay = nowDayOfWeek != 0 ? nowDayOfWeek - 1 : 6; //减去的天数
  let monday = new Date(timeStamp - minusDay * millisecond); //本周 周一
  let sunday = new Date(monday.getTime() + 6 * millisecond); //本周 周日
  let sy = monday.getFullYear();
  let sm = monday.getMonth() + 1;
  let sd = monday.getDate();
  let ey = sunday.getFullYear();
  let em = sunday.getMonth() + 1;
  let ed = sunday.getDate();
  // 需要时分秒
  // startTime = sy + "-" + monthFormat(sm) + "-" + monthFormat(sd) + " 00:00:00"; //开始
  // endTime = ey + "-" + monthFormat(em) + "-" + monthFormat(ed) + " 23:59:59"; //结束
  // 不需要时分秒
  let startTime = sy + "-" + monthFormat(sm) + "-" + monthFormat(sd); //开始
  let endTime = ey + "-" + monthFormat(em) + "-" + monthFormat(ed); //结束
  let weekDateRange = [startTime, endTime];
  // 月份格式化, 如果是个位数, 需要在前面加一个0
  function monthFormat(month) {
    return month < 10 ? "0" + month : month;
  }
  return weekDateRange;
}

//格式化日期：yyyy-MM-dd
function formatDate(date) {
  let myyear = date.getFullYear();
  let mymonth = date.getMonth() + 1;
  let myweekday = date.getDate();

  if (mymonth < 10) {
    mymonth = "0" + mymonth;
  }
  if (myweekday < 10) {
    myweekday = "0" + myweekday;
  }
  return myyear + "-" + mymonth + "-" + myweekday;
}

function getQueryString(name) {
  var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
  var r = window.location.search.substr(1).match(reg);
  if (r != null) {
    return unescape(r[2]);
  } else {
    var strHash = window.location.hash;
    if (strHash && strHash.length > 0 && strHash.indexOf("?") > -1) {
      r = strHash.substr(strHash.indexOf("?") + 1).match(reg);
    }
    if (r != null) {
      return unescape(r[2]);
    }
  }
  return null;
}

let $com_util = {
  formatTime: formatTime,
  formatCurrency: formatCurrency,
  updateQueryStringParameter: updateQueryStringParameter,
  random_string: random_string,
  replaceStr: replaceStr,
  getNewDate: getNewDate,
  getNowDateTime,
  dateToTimeStamp,
  getLatelyTodayDate,
  getLatelyYesterdayDate,
  getMonthStartDateAndMonthEndDate,
  getQuarterStartDateAndMonthEndDate,
  getWeekStartDateAndMonthEndDate,
  getQueryString,
};

export { $com_util };
